from rest_framework import viewsets
from .models import Department, Contact
from .serializers import DepartmentSerializer, ContactSerializer
from django.shortcuts import render
from .models import Contact
from django.core.paginator import Paginator

class DepartmentViewSet(viewsets.ModelViewSet):
    queryset = Department.objects.all()
    serializer_class = DepartmentSerializer

class ContactViewSet(viewsets.ModelViewSet):
    queryset = Contact.objects.filter(is_displayed=True)
    serializer_class = ContactSerializer

def home_view(request):
    contacts = Contact.objects.filter(is_displayed=True)
    return render(request, 'home.html', {'contacts': contacts})

def contact_list(request):
    contact_list = Contact.objects.select_related('department').filter(is_displayed=True)
    paginator = Paginator(contact_list, 20)  # 每页20条
    page = request.GET.get('page')
    contacts = paginator.get_page(page)
    return render(request, 'django_txlapp/contact_list.html', {'contacts': contacts})
